/*
Copyright (c) 2010 Neil Wood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package envisage.Data
{
	public class ArraySetFlags extends ArrayView
	{
		public function ArraySetFlags(iDataArray:IArray, fieldName:String)
		{
			super(iDataArray);
			
			this.fieldName = fieldName;
			
			Refresh();
		}
		
		public static const KEEP_EXACT:int 	 = 0;
		public static const KEEP_ANY:int 	 = 1;
		public static const EXCLUDE_ANY:int  = 2;
		
		public function SetMode(mode:int):void
		{
			if( this.mode != mode )
			{
				this.mode = mode;
				Refresh();
			
				if( iArrayListener != null ) iArrayListener.ItemsReplaced();
			}
		}
		
		private var fieldName:String;
		private var flags:uint = 0;//0xFFFFFFFF;
		private var mode:int;
		
		public function GetFlags():uint { return flags; }
		
		public function SetFlags(flags:uint):void
		{
			if( this.flags != flags )
			{
				this.flags = flags;
				Refresh();
			
				if( iArrayListener != null ) iArrayListener.ItemsReplaced();
			}
		}
		
		private function Refresh():void
		{
			arrIndicies.length = 0;
			
			var i:int;
			var o:Object;
			var f:uint;
			
			var len:int = iDataArray.GetNumItems();
			
			switch(mode) {
			case KEEP_EXACT:
				for(i = 0 ; i < len ; i++)
				{
					o = iDataArray.GetItem(i);
					f = uint(o[fieldName]);
					if( (f & flags) == flags ) arrIndicies.push(i);	
				}
				break;
			
			case KEEP_ANY:
				for(i = 0 ; i < len ; i++)
				{
					o = iDataArray.GetItem(i);
					f = uint(o[fieldName]);
					if( (f & flags) != 0 ) arrIndicies.push(i);	
				}
				break;
			
			case EXCLUDE_ANY:
				for(i = 0 ; i < len ; i++)
				{
					o = iDataArray.GetItem(i);
					f = uint(o[fieldName]);
					if( (f & flags ) == 0 ) arrIndicies.push(i);
				}
				break;
			}
					
		}
		
		//will need to implement a couple of these.
		public override function ItemsRearranged():void
		{
			Refresh();
			if( iArrayListener != null ) iArrayListener.ItemsReplaced();
			//not actually true, as long as set can be in different order
		}
		public override function ItemsReplaced():void
		{
			Refresh();
			if( iArrayListener != null ) iArrayListener.ItemsReplaced();
		}
		
		public override function ItemInserted(index:int):void {throw "";}
		public override function ItemRemoved(index:int):void {throw "";}
		public override function ItemUpdated(index:int):void {throw "";}
	}
}


